//
//  MMainTabBar.swift
//  iOSweibo
//
//  Created by lzm on 16/5/10.
//  Copyright © 2016年 lzm. All rights reserved.
//

import UIKit

class MMainTabBar: UITabBar {
    //MARK -- 属性
    //3.定义加号按钮闭包
    var composeCallBack: (() -> ())?
    
    
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let width = self.frame.width / 5
        var index = 0
        for view in self.subviews{
            if view.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                view.frame = CGRect(x: index > 1 ? width * CGFloat(index+1) : width * CGFloat(index), y: 0, width: width, height: self.frame.height)
            
            index++
            }
        }
        
        composeBtn.frame = CGRect(x: 2 * width, y: 0, width: width, height: self.frame.height )
    }
    
    
    // MARK: -- 点击时间
    func composeClick(btn:UIButton)
    {
        //4.调用闭包
        composeCallBack?()
    }
    
    
    // MARK: -- 来加载
    lazy var composeBtn :UIButton = {
        let btn = UIButton(type: UIButtonType.Custom)
        //设置背景图片
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        //设置图片
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        //设置frame
        self.addSubview(btn)
        
        btn.addTarget(self, action: "composeClick:", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()
}


